package com.example.common.dto;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：czx
 * @date ：Created in 2021/6/24 15:18
 * @modified By：
 */
public class TransResult {

    /*
                 需要返回的对象
     */
    private Object object;
    /*
                 和第三方服务交互返回交易编码
     * 1:交易处理成功
     * 0:交易处理失败
     * -1:交易处理发生内部异常
     */
    private String resultCode = "1";
    /*

     */
    private String resultInfo = "";
    /*
                 和第三方服务交互返回交易描述
     */
    private String resultInfoDesc = "交易处理成功";
    /*
                 验证失败错误信息实体类集合
     */
    private List<ValidateError> validateErrors;
    /*
                 执行期间发生异常,封装异常消息返回给调用端
     *
     * @param ex
     */
    public void addException(Exception ex) {
        this.setResultCode("-1");
        StringWriter sw = new StringWriter();
        ex.printStackTrace(new PrintWriter(sw));
        this.setResultInfoDesc(sw.toString());
    }
    /*
                 添加错误到错误队列
     *
     * @param code 错误代码
     * @param msg 错误信息
     */
    public void addValidateError(String code, String msg) {
        ValidateError error = new ValidateError();
        error.setCode(code);
        error.setMessage(msg);
        this.addValidateError(error);
    }
    /*
                  添加错误到错误队列
     *
     * @param validateError
     */
    public void addValidateError(ValidateError validateError) {
        if (this.validateErrors == null) {
            this.validateErrors = new ArrayList<ValidateError>();
        }
        this.resultCode = "0";
        this.resultInfoDesc = "交易处理失败";
        this.validateErrors.add(validateError);
    }
    /*
                 执行交易发生异常
     */
    public void exception() {
        this.setResultCode("-1");
        this.setResultInfoDesc("交易处理异常");
    }
    /*
              执行交易失败
                并发发生异常情况，失败指发生逻辑校验未通过的情况
     */
    public void failure() {
        this.setResultCode("0");
        this.setResultInfoDesc("交易处理失败");
    }
    /*
              执行交易失败
               并发发生异常情况，失败指发生逻辑校验未通过的情况
     */
    public void failure(String errorMsg) {
        this.setResultCode("0");
        this.setResultInfoDesc(errorMsg);
    }
    public Object getObject() {
        return object;
    }
    public String getResultCode() {
        return resultCode;
    }
    public String getResultInfo() {
        return resultInfo;
    }
    public String getResultInfoDesc() {
        return resultInfoDesc;
    }
    public List<ValidateError> getValidateErrors() {
        return validateErrors;
    }
    /*
                判断是否有错误
     *
     * @return true 发生错误
     */
    public boolean hasErrors() {
        if (this.validateErrors != null && this.validateErrors.size() > 0) {
            return true;
        }
        else {
            return false;
        }
    }
    /*
                   判断服务是否发生异常
     *
     * @return true 发生异常
     */
    public boolean hasException() {
        if (this.resultCode.equals("-1")) {
            return true;
        }
        else {
            return false;
        }
    }
    /*
                  判断交易是否成功
     *
     * @return true 交易成功 , false 交易失败(非异常)
     */
    public boolean isSuccess() {
        if (this.resultCode.equals("1")) {
            return true;
        }
        else {
            return false;
        }
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public void setResultInfo(String resultInfo) {
        this.resultInfo = resultInfo;
    }

    public void setResultInfoDesc(String resultInfoDesc) {
        this.resultInfoDesc = resultInfoDesc;
    }

    public void setValidateErrors(List<ValidateError> validateErrors) {
        this.resultCode = "0";
        this.resultInfoDesc = "交易处理失败";
        this.validateErrors = validateErrors;
    }

    /*
               执行交易成功
     */
    public void success() {
        this.setResultCode("1");
        this.setResultInfoDesc("交易处理成功");
    }
}
